package com.gr.staffpm.tasks.service;

import java.util.Iterator;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;

import com.gr.staffpm.datatypes.Project;
import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.projects.service.ProjectService;
import com.gr.staffpm.tasks.model.DetachableTaskModel;

/**
 * @author Graham Rhodes 16 Jan 2011 16:23:00
 */
public class SortableTaskDataProvider extends SortableDataProvider<Task> {

    private ProjectService projectService = null;
    private Project project = null;
    private static final long serialVersionUID = 1L;

    public SortableTaskDataProvider(ProjectService projectService, Project project) {
        //Set default sort
        setSort(Task.NAME, true);
        this.projectService = projectService;
        this.project = project;
    }

    /**
     * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int, int)
     */
    @Override
    public Iterator<? extends Task> iterator(int first, int count) {
        SortParam sp = getSort();
        return projectService.getTasks(project, first, count, sp.getProperty(), sp.isAscending()).iterator();
    }

    @Override
    public IModel<Task> model(Task task) {
        return new DetachableTaskModel(projectService, task);
    }

    @Override
    public int size() {
        return projectService.getAllTasks(project).size();
    }

}
